layui.use(['form'], function () {
  var form = layui.form
      , layer = layui.layer
      , $ = layui.$;

  // 获取到初始化的值
  var initArea = $("#city").val();
  syncGetArea(initArea);

  //监听提交
  form.on('submit(demo1)', function (data) {
    var requestUrl = $("#requestUrl").val();
    $.ajax({
      url: '/' + requestUrl + '/apply/add',
      type: 'POST',
      dataType: 'JSON',
      contentType: "application/json;charset=UTF-8",
      data: JSON.stringify(data.field),
      success: function (result) {
        if (result.code == 200) {
          layer.msg(result.msg);
          // 更新验证码
          /// $("#verifyCodeImg").attr("src", "/apply/verifyCode?" + new Date().getTime())
          // 跳转到成功页面
          window.location.href = '/apply/success/' + requestUrl;
        } else {
          layer.msg(result.msg);
          return false;
        }
      }
    })
  });

  // 异步获取区县
  form.on('select(city)', function (data) {
    var city = data.value;
    syncGetArea(city);
  })

  // 公用方法（异步获取地市）
  function syncGetArea(city) {
    if (!city) {
      return false;
    }
    $.ajax({
      url: '/region/getByFatherRegionId',
      type: 'POST',
      data: {regionId: city},
      success: function (result) {
        if (result.code == 200) {
          $("select[name=district]").html("");
          var optionEle = "";
          for (var i in result.data) {
            optionEle += '<option value="' + result.data[i].regionId + '">' + result.data[i].regionName + '</option>';
          }
          $("select[name=district]").append(optionEle)
          form.render();
          return;
        } else {
          $("#city").val("");
          layer.msg(result.msg)
        }
      }
    })
  }

  // 更新验证码
  $("#verifyCodeImg").click(function () {
    $("#verifyCodeImg").attr("src", "/apply/verifyCode?" + new Date().getTime())
  });

  // 上传图片
  $(document).on('change', '#businessLicense', function () {
    var editImage = $("#businessLicense").get(0).files[0];
    console.log(editImage);
    var formDate = new FormData();
    formDate.append("file", editImage);
    $.ajax({
      url: '/apply/fileUpload',
      type: 'POST',
      processData: false,
      contentType: false,
      async: false,
      data: formDate,
      success: function (result) {
        if (result.code == 200) {
          $("#busLicense").val("");
          $("#busLicense").val(result.data);
          layer.msg(result.msg)
        } else {
          $("#busLicense").val("");
          layer.msg(result.msg)
        }
      }
    })
  })

  // 验证手机号
  $(document).on('blur', '#phone', function () {
    var phone = $("#phone").val();
    if (!isPhoneNo(phone)) {
      layer.msg("请输入正确的手机号！");
      return false;
    }
  });

  // 验证手机号
  function isPhoneNo(phone) {
    var pattern = /0?(13|14|15|18|17)[0-9]{9}/;
    return pattern.test(phone);
  }

  // 校验验证码
  $(document).on('blur', '#verifyCode', function () {
    var verifyCode = $("#verifyCode").val();
    $.ajax({
      url: '/apply/checkVerifyCode',
      type: 'POST',
      dataType: 'JSON',
      data: {
        verifyCode: verifyCode
      },
      success: function (result) {
        if (result.code == 200) {
          return true;
        } else {
          layer.msg(result.msg)
          return false;
        }
      }
    })
  });


  // 返回继续填写
  $(document).on('click', '#backHistoryAndFlushPage', function () {
    var returnRequestUrl = $("#returnRequestUrl").val().trim();
    window.location.href = '/' + returnRequestUrl + '/apply/page';

  });

});